package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.StoreinfoDao;
import cn.ym.mall.entity.Staff;
import cn.ym.mall.entity.Storeinfo;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 赵金明
 * 2022/8/11
 */
public class StoreinfoDaoImpl implements StoreinfoDao{
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 新增商店信息
     *
     * @param storeinfo 添加的订单
     * @return 返回成功的条数
     */
    @Override
    public Integer addStoreinfo(Storeinfo storeinfo)throws SQLException {
        int num=0;
        String sql="insert into ym_storeinfo(snmae,smanage,sregistrationQualification,sregisteredFund,scompanyTel,semail,sstaff,saddress,sbusinessLicencese,sbankName,sbankAccount,sauditState)" +
                " value(?,?,?,?,?,?,?,?,?,?,?,?);";
        Object arr[] ={storeinfo.getSname(),storeinfo.getSmanage(),storeinfo.getSregistrationQualification(),storeinfo.getSinfoisregisteredFund(),
                storeinfo.getScompanyTel(),storeinfo.getSemail(),storeinfo.getSstaff(),storeinfo.getSaddress(),
                storeinfo.getSisbusinessLicensenfoid(),storeinfo.getSinfoisbankNamed(),storeinfo.getSinfoisbankAccountd(),storeinfo.getSsauditState()};
        num=queryRunner.update(DruidUtils.getConnection(),sql,arr);
        return num;
    }

    /**
     * 根据注册信息编号删除商店信息
     *
     * @param sinfoid 订单
     * @return 返回删除条数
     */
    @Override
    public Integer deleteBySinfoid(String sinfoid)throws SQLException {
        int num = 0;
        String sql = "delete from ym_storeinfo where sinfoid = ?;";
        num = queryRunner.update(DruidUtils.getConnection(), sql, sinfoid);
        return num;
    }

    /**
     * 通过注册信息编号查询注册信息
     *
     * @param sinfoid
     * @return
     */
    @Override
    public Storeinfo getStoreinfoBysinfoid(String sinfoid)throws SQLException {
        String sql = "select * from ym_storeinfo where sinfoid = ?;";
        return queryRunner.query(DruidUtils.getConnection(), sql, new BeanHandler<Storeinfo>(Storeinfo.class), sinfoid);
    }

    /**
     * 查询所有的商店注册信息
     * 带分页
     * @return 注册信息集合
     */
    @Override
    public List<Storeinfo> getAllStoreinfo(Integer pageNo, Integer pageSize)throws SQLException {
        String sql="select * from ym_storeinfo order by sthiredate desc limit ?,?;";
        List<Storeinfo> storeinfoList = new ArrayList<Storeinfo>();
        try {
            storeinfoList=queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Storeinfo>(Storeinfo.class),(pageNo-1)*pageSize,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return storeinfoList;
    }

    /**
     * 查询商店信息总数
     *
     * @return
     */
    @Override
    public long getStoreinfoCount()throws SQLException {
        long count = 0;
        try{
            String sql = "select count(0) as count from ym_storeinfo;";
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<>());
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 修改注册信息
     *
     * @param order 订单
     * @return 返回更新成功的条数
     */
    @Override
    public Integer updateStoreinfo(Storeinfo storeinfo)throws SQLException {
        String sql="update ym_storeinfo set smanage =?,sregistrationQualification=?,sregisteredFund=?,scompanyTel=?,semail=?," +
                "sstaff=?,saddress=?,sbusinessLicencese=?,sbankName=?,sbankAccount=?,sauditState=? where sinfoid=?;";
        Object arr[] ={storeinfo.getSname(),storeinfo.getSmanage(),storeinfo.getSregistrationQualification(),storeinfo.getSinfoisregisteredFund(),
                storeinfo.getScompanyTel(),storeinfo.getSemail(),storeinfo.getSstaff(),storeinfo.getSaddress(),
                storeinfo.getSisbusinessLicensenfoid(),storeinfo.getSinfoisbankNamed(),storeinfo.getSinfoisbankAccountd(),storeinfo.getSsauditState()};
        int num=0;
        try {
            num=queryRunner.update(DruidUtils.getConnection(),sql,new BeanListHandler<Storeinfo>(Storeinfo.class),arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
}
